package cn.tedu.fruitshop.service.impl;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.mapper.CarMapper;
import cn.tedu.fruitshop.pojo.dto.CarAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.CarUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.entity.Car;
import cn.tedu.fruitshop.pojo.vo.CarDetailsVO;
import cn.tedu.fruitshop.pojo.vo.CarListItemVO;
import cn.tedu.fruitshop.pojo.vo.CarStandardVO;
import cn.tedu.fruitshop.pojo.vo.CarTotalPriceAndTotalWeight;
import cn.tedu.fruitshop.service.ICarService;
import cn.tedu.fruitshop.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CarServiceImpl implements ICarService {

    @Autowired
    CarMapper carMapper;

    @Override
    public void addNew(CarAddNewDTO carAddNewDTO) {
        log.debug("开始处理【添加购物车】的业务，参数：{}", carAddNewDTO);
        Car car = new Car();
        BeanUtils.copyProperties(carAddNewDTO, car);
        Car carUpdate = carMapper.getByUserIdAndItemId(car.getUserId(), car.getItemId());
        if (carUpdate != null) {
            carUpdate.setWeight(carUpdate.getWeight() + carAddNewDTO.getWeight());
            carMapper.update(carUpdate);
        } else {
            log.debug("即将插入购物车数据：{}", car);
            int rows = carMapper.insert(car);
            if (rows != 1) {
                String message = "服务器老了,添加购物车数据失败";
                throw new ServiceException(ServiceCode.ERR_INSERT, message);
            }
        }

        log.debug("插入购物车数据完成");
    }

    @Override
    public void delete(Integer id) {
        CarStandardVO queryResult = carMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除购物车失败,尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int rows = carMapper.deleteById(id);
        if (rows != 1) {
            String message = "服务器老了,删除购物车数据失败";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public List<CarDetailsVO> list(Integer userId) {
        List<CarDetailsVO> carDetailsVOS = carMapper.listByUserId(userId);
        log.info("购物车:{}", carDetailsVOS);
        return carDetailsVOS;
    }

    @Override
    public void updateInfoById(Integer id, CarUpdateInfoDTO carUpdateInfoDTO) {
        log.debug("开始处理【修改购物车详情】的业务，参数ID：{}, 新数据：{}", id, carUpdateInfoDTO);
        CarStandardVO queryResult = carMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改购物车详情失败！尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Car car = new Car();
        BeanUtils.copyProperties(carUpdateInfoDTO, car);
        car.setId(id);
        // 修改数据
        log.debug("即将修改数据：{}", car);
        int rows = carMapper.update(car);
        if (rows != 1) {
            String message = "修改类别详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void update(CarDetailsVO carDetailsVO) {
        Car car = new Car();
        BeanUtils.copyProperties(carDetailsVO, car);
        log.info("car={}", car);
        carMapper.update(car);
    }

    @Override
    public CarTotalPriceAndTotalWeight getTotalByUserId(Integer userId) {
        List<CarDetailsVO> carDetailsVOS = carMapper.listByUserId(userId);
        Double totalPrice = 0.0;
        Double totalWeight = 0.0;
        for (CarDetailsVO carDetailsVO : carDetailsVOS) {
            totalPrice += carDetailsVO.getPrice();
            totalWeight += carDetailsVO.getWeight();
        }
        CarTotalPriceAndTotalWeight carTotalPriceAndTotalWeight = new CarTotalPriceAndTotalWeight(userId, totalPrice, totalWeight);
        return carTotalPriceAndTotalWeight;
    }

    @Override
    public void deleteByUesrId(Integer userId) {
        carMapper.deleteByUserId(userId);
    }


    public Integer getCount(Integer userId) {
        log.info("根据用户id查询购物车商品数量");
        Integer count = carMapper.countByUserId(userId);
        return count;
    }
}
